#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <cstring>
#include <unistd.h>
#include <cerrno>
using namespace std;
#define PATH "/home/user100"
#define proj_id 111
#define SIZE 4096
int main()
{
    key_t key = ftok(PATH, proj_id);
    if (key < 0)
    {
        cout << "ftok fail errno is " << errno << " error string is " << strerror(errno) << endl;
        return 1;
    }
    int shmid = shmget(key, SIZE, IPC_CREAT | IPC_EXCL | 0666);
    if (shmid < 0)
    {
        cout << "shmid fail errno is " << errno << " error string is " << strerror(errno) << endl;
        return 2;
    }
    cout << "make shm success" << endl;
    sleep(2);
    void *addr = shmat(shmid, nullptr, 0);
    if ((long long)addr == -1)
    {
        cout << "shmat fail errno is " << errno << " error string is " << strerror(errno) << endl;
    }
    cout << "shmat success" << endl;
    sleep(5);
    int ret1 = shmdt(addr);
    if (ret1 < 0)
    {
        cout << "shmdt fail errno is " << errno << " error string is " << strerror(errno) << endl;
    }
    cout << "shmdt success" << endl;
    sleep(2);

    int ret = shmctl(shmid, IPC_RMID, nullptr);
    if (ret < 0)
    {
        cout << "remove shm fail errno is " << errno << " error string is " << strerror(errno) << endl;
        return 3;
    }

    cout << "remove shm success,shmid is " << shmid << endl;
    return 0;
}